Ext.ns('App.module.Page');
App.module.Page.Update = Ext.extend(Ext.util.Observable, {
	constructor: function(config){
		App.module.Page.Add.superclass.constructor.call(this);	
	}
	,init: function(grid){
		this.grid = grid;
		grid.on('render', function(grid){
			var bt = new Ext.Button({
				tooltip: 'Add'
				,iconCls: 'icon-update-16x16'
				,handler: this.showWin
				,scope: this
			}, this);
			grid.getTopToolbar().add(bt);
			
			grid.selModel.on('rowselect', function(sm, index, rec){
				var win = this.getWin();
				if(!win.isVisible()) return;
				this.load(rec.data.page_id);
			}, this);
		}, this);
	}
	,getForm: function(){
		if(this.form) return this.form;
		
		var grid = this.grid;
		var o = this;
		var form = new Ext.FormPanel({
			monitorValid: true
			,frame: false
			,baseCls: 'x-plain'
			,region: 'center'
			,defaults: {anchor: '-10'}
			,api: {
				load: API.page.detail	
			}
			,items: [
				{
					xtype: 'hidden'
					,name: 'page_id'
				},{
					xtype: 'textfield'
					,name: 'name'
					,fieldLabel: 'Name'
					,allowBlank: false
				},{
					xtype: 'textarea'
					,name: 'content'
					,hideLabel: true
					,anchor: '-10, -30'	
				}
			]
			,buttons: [
				{
					text: 'Save'
					,formBind: true
					,handler: function(bt){
						form.el.mask('Processing...');
						var values = form.getForm().getValues();
						API.page.update(values, function(r){
							form.el.unmask();
							if(!r) return Ext.Msg.alert('Error', 'Unknow error!');
							if(!r.success) return Ext.Msg.alert('Error', r.msg);
							
							var store, index, record;
							if(!(store = grid.getStore())) return;
							if(!(index = store.findExact('page_id', values.page_id))) return;
							if(!(record = store.getAt(index))) return;
							
							record.set('name', values.name);
							record.commit();
						});
					}
				},{
					text: 'Reset'
					,handler: function(){
						form.getForm().reset();
						form.getForm().findField('page_id').setValue(grid.selModel.getSelected().data.page_id);
					}
				},{
					text: 'Close'
					,handler: function(){o.getWin().hide();}	
				}
			]
		});
		this.form = form;
		return this.form;
	}
	,load: function(page_id){
		var form = this.getForm();
		form.el.mask('Loading...');
		API.page.detail({page_id: page_id}, function(r){
			form.el.unmask();
			if(!r.success){
				Ext.Msg.alert('Error', r.msg);
				return;
			}
			r.data.page_id = page_id;
			form.getForm().loadRecord(new Ext.data.Record(r.data));
		}, this);
	}
	,getItems: function(){
		if(this.items) return this.items;
		this.items = [
			this.getForm()
		];
		return this.items;	
	}
	,getWin: function(){
		if(this.win) return this.win;
		
		var items = this.getItems();
		this.win = new App.SubWindow({
			title: 'Update Page'
			,renderTo: this.grid.id
			,plain: true
			,items: items
			,bodyStyle: 'padding:5px'
			,layout: 'border'
			,height: 300
			
		});
		
		this.win.on('show', function(win){
			var page_id = this.grid.selModel.getSelected().data.page_id;
			this.load(page_id);
		}, this);
		
		return this.win;
	}
	,showWin: function(bt){
		this.getWin().show(bt.id);
	}
});